package com.itany.boot.controller;

import com.itany.boot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/5/27 10:24
 * Version:1.0-SNAPSHOT
 * Description:
 */
@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/f1")
    public ModelAndView f1(HttpSession session){
        ModelAndView mav = new ModelAndView();

        mav.setViewName("tf");
        mav.addObject("msg","<h1>Hello Thymeleaf</h1>");

        User user = new User(1,"admin","123456","13812345678","江苏-南京");
        User user2 = new User(3,"jack","123456","13812345678","江苏-南京");
        User user3 = new User(6,"tom","123456","13812345678","江苏-南京");
        User user4 = new User(7,"alice","123456","13812345678","江苏-南京");
        session.setAttribute("user",user);

        ServletContext application = session.getServletContext();
        application.setAttribute("user",user2);

        mav.addObject("id",2);

        mav.addObject("age",20);

        mav.addObject("role","student");

        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        mav.addObject("users",users);
        mav.addObject("user",user);

        return mav;
    }

}
